<?php
/**
 * Created by PhpStorm.
 * AdminUser: Administrator
 * Date: 2020/11/24
 * Time: 17:44
 */
namespace app\user\controller;

use app\admin\controller\AdminBase;
use app\common\model\Appmenu;
use app\common\model\RoleUser;
use app\common\model\User;
use think\facade\Db;
use think\facade\View;
use think\facade\Request;

class AdminUser extends AdminBase
{
    public $user_id;
    private $per_page = 10;

    public function index()
    {
        //$page_no           = Request::param('page_no', 1);
        $search_filter  = Request::param('search_filter', array());
        $status         = Request::param('status', 1);

        $list = getList('user', ['type'=>20,'status'=>$status]);

        return View::fetch('adminuser/index',[
            'list'          => $list,
            'status'        => $status,
            'search_filter' => $search_filter,
        ]);
    }
    /**
     * 添加
     */
    public function add()
    {
        return View::fetch('add');
    }

    public function save()
    {
        $id        = Request::param('id', 0);
        $info      = Request::param('info', array());
        $password  = Request::param('password');
        $role      = Request::param('role', 0);

        // 如果用户存在则查询hash否则生成
        if ($id) {
            $info['hash'] = User::random_hash();
            $user_info    = User::get_user_info($id);
            if (!$user_info) {
                return '要编辑的信息不存在';
            }
        }

        // 过滤搜索条件
        foreach ($info as $k=>$v) {
            // 过滤掉数字型的值
            if (!is_numeric($v)) {
                $info[$k] = trim($v);
            }
        }

        // 名字
        if (!isset($info['user_name']) || !$info['user_name']) {
            return _return('用户名不能为空');
        }
        // 用户名不能重复
        $user_info = _uri('user',['user_name' => $info['user_name']]);
        if ($user_info && $user_info['id'] != $id) {
            return _return('用户名已经存在，请重新输入');
        }

        // 新用户必须设置密码
        if (!$id && !$password) {
            return _return('请为新用户设置密码');
        }

        // 如果添加 或者修改密码
        if ($password) {
            $password_pattern = '/^[a-zA-Z0-9]{5,14}/';
            if (!preg_match($password_pattern, $password)) {
                return _return('密码格式不正确');
            }

            $info['hash']     = User::random_hash();
            $info['password'] = User::set_pwd($password, $info['hash']);
        }

        $info['type'] = 20;

        if ($id) {
            $where = ['id' => $user_info['id']];
            User::update($info,$where);

            RoleUser::update(['role_id'=>$role], ['user_id'=>$id]);
        } else {
            $user = User::create($info);
            RoleUser::create(['user_id' => $user->id, 'role_id' => $role]);
        }

        return redirect(url('/user/adminuser/index'));
    }

    /**
     * 删除
     * @return string
     */
    public function delete()
    {
        $id = Request::param('id');
        if (!$id) {
            return ['info' => '对不起，请选择您要删除的内容库！'];
        }

        $ids = explode(',', trim($id, ','));
        foreach ($ids as $v) {
            $user_info = _uri('user', ['id' => $v]);
            if (!$user_info) {
                continue;
            }

            if ($user_info['status'] == 0) {

                Appmenu::where(['id'=>$v])->delete();

            } else {
                Appmenu::update(['status'=>0], ['id'=>$v]);
            }
        }

        return ['info' => "ok"];
    }

    /**
     * 还原
     * @return string
     */
    public function change_status()
    {
        $id = Request::param('id');

        if (!$id) {
            return ['info' => '请选择要还原的标签信息'];
        }

        $ids = explode(',', trim($id, ','));

        foreach ($ids as $v) {
            $status = _uri('user', ['id' => $v], 'status');

            if ($status['status'] == 0) {
                Appmenu::update(['status'=>1], ['id'=>$v]);
            }
        }

        return array('info'=>'ok');
    }
}